const gulp = require('gulp');
const gulpUglify = require('gulp-uglify');
const gulpCssmin = require('gulp-cssmin');
const gulpHtmlmin = require('gulp-htmlmin');
const rev = require('gulp-rev-append');
const notify = require("gulp-notify");
const rename = require("gulp-rename");
 
 
const DEV_PATH = './dev';
const DIST_PATH = './dist';

gulp.task('default', function () {
	console.log('gulp default task');
});

// 压缩并发布js文件
gulp.task('dist-js', function () {
	gulp.src([DEV_PATH + '/js/*.js','!'+DEV_PATH+'/js/{config,config-dist,config-dev,config-test}.js'])
	.pipe(gulpUglify())
	.pipe(gulp.dest(DIST_PATH + '/js'))
	.pipe(notify({message: 'Js task complete'}));
});

//修改正式环境config文件名
gulp.task('dist-config', function () {
	gulp.src(DEV_PATH + '/js/config-dist.js')
	.pipe(rename("config.js"))
	.pipe(gulp.dest(DIST_PATH + '/js'));
});

//修改测试环境config文件名
gulp.task('dev-config', function () {
	gulp.src(DEV_PATH + '/js/config-dev.js')
	.pipe(rename("config.js"))
	.pipe(gulp.dest(DIST_PATH + '/js'));
});

// 压缩并发布css及ttf文件
gulp.task('dist-css', function () {
	gulp.src(DEV_PATH + '/css/*.css')
	.pipe(gulpCssmin())
	.pipe(gulp.dest(DIST_PATH + '/css'));

	gulp.src(DEV_PATH + '/fonts/*.ttf')
	.pipe(gulp.dest(DIST_PATH + '/fonts'))
	.pipe(notify({message: 'Styles task complete'}));
});

// 压缩并发布html文件
gulp.task('dist-html', function () {
	// 常用的配置
//	var options = {
//		removeComments: false, //清除HTML注释
//		collapseWhitespace: false, //压缩HTML
//		collapseBooleanAttributes: false, //省略布尔属性的值 <input checked="true"/> ==> <input />
//		removeEmptyAttributes: false, //删除所有空格作属性值 <input id="" /> ==> <input />
//		removeScriptTypeAttributes: false, //删除<script>的type="text/javascript"
//		removeStyleLinkTypeAttributes: false, //删除<style>和<link>的type="text/css"
//		minifyJS: true, //压缩页面JS
//		minifyCSS: true //压缩页面CSS
//	};
	gulp.src(DEV_PATH + '/pages/*.html')
	.pipe(rev())
//	.pipe(gulpHtmlmin(options))
	.pipe(gulp.dest(DIST_PATH + '/pages'))
	.pipe(notify({message: 'Html task complete'}));
});

// 发布其它文件，例如manifest.json等；
gulp.task('dist-other', function () {
	gulp.src(DEV_PATH + '/manifest.json')
	.pipe(gulp.dest(DIST_PATH));
});

// 正式环境发布应用
gulp.task('dist-app', ['dist-js', 'dist-css', 'dist-html', 'dist-other','dist-config']);

//测试环境发布应用
gulp.task('dev-app', ['dist-js', 'dist-css', 'dist-html', 'dist-other','dev-config']);